package task;

import java.awt.Component;
import java.awt.Font;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.Map;

import javax.swing.JScrollPane;
import javax.swing.JTextArea;

// This class displays the posted messages IF the message's
// priority passes the filter.
public final class TaskMessages implements TaskEventListener {
	// Member Methods

	public TaskMessages(int level) {
		TaskController control = new TaskController();
		Locale currentLocale = new Locale("en", "US");

		// Put together the date/time output format.
		_timestampFormat = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss",
				currentLocale);

		// Set the initial level.
		_level = level;

		// Create the text area which displays the messages.
		_messageArea = new JTextArea(ROW_COUNT, COL_COUNT);
		_messageArea.setEditable(false);

		// Use a 8pt Helvetica font.
		_messageArea.setFont(new Font("Courier", Font.PLAIN, 10));

		// Place this text area inside a scrollable pane.
		_pane = new JScrollPane(_messageArea);

		control.register("Message GUI", this);
	}

	public int getLevel() {
		return (_level);
	}

	public void setLevel(int level) {
		_level = level;
		return;
	}

	public Component getComponents() {
		return ((Component) _pane);
	}

	public void handleEvent(String event, Map<String, Object> args) {
		Integer level;
		String name;
		String message;

		// Post a message to the text area.
		if (event.compareTo("Post Message") == 0) {
			level = (Integer) args.get("level");
			name = (String) args.get("object");
			message = (String) args.get("message");
			logMessage(level.intValue(), name, message);
		} else if (event.compareTo("Set Level") == 0) {
			level = (Integer) args.get("level");
			setLevel(level.intValue());
		}
		return;
	}

	public void logMessage(int level, String name, String message) {
		int stringLength;
		Date currTime = new Date();
		String timestamp;
		StringBuffer output = new StringBuffer(80);

		// Display the message only if it passes the level
		// filter.
		if (level <= _level) {
			// Put together the message line:
			// MM/DD/YYYY hh:mm:ss name message
			// where:
			// + hh is in military time
			// + name is 15 characters
			// + message is at most 44 characters.
			timestamp = _timestampFormat.format(currTime);

			output.append(timestamp + " ");

			// Make certain that the object name does not exceed
			// max size.
			if ((stringLength = name.length()) > NAME_LEN) {
				stringLength = NAME_LEN;
			}

			output.append(name.substring(0, stringLength));

			// Pad to the message field's beginning.
			output.append(_padding.substring(0, (NAME_LEN - stringLength + 1)));

			output.append(message);
			output.append(_newLine);

			// Put the log message up on display.
			_messageArea.append(output.toString());
		}
		return;
	}

	// Member Data

	// Currently acceptable message level.
	private int _level;

	// Put messages in this text component.
	private JTextArea _messageArea;

	// The messages are scrollable.
	private JScrollPane _pane;

	// Formats the date/time string.
	private SimpleDateFormat _timestampFormat;

	// The number of rows and columns in the text area.
	private final static int ROW_COUNT = 10;
	private final static int COL_COUNT = 82;

	// Each field's length.
	public final static int NAME_LEN = 15;

	// The padding and new line strings are used to create the
	// final message output.
	private static String _padding;
	private static String _newLine;

	static {
		_padding = new String("                ");
		_newLine = new String("\n");
	}
}
